package pers.qianyu.month_202012.date_20201226;

import org.junit.*;

import java.util.*;

/**
 * 135. 分发糖果
 * https://leetcode-cn.com/problems/candy/
 *
 * @author mizzle rain
 * @date 2020-12-26 16:55
 */
public class Candy {
    public int candy(int[] ratings) {
        int len = ratings.length;
        if (len == 0) {
            return 0;
        }
        int[] left = new int[len];
        int[] right = new int[len];
        Arrays.fill(left, 1);
        Arrays.fill(right, 1);
        for (int i = 1; i < len; i++) {
            if (ratings[i] > ratings[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
        }
        for (int i = len - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                right[i] = right[i + 1] + 1;
            }
        }
        int ans = 0;
        for (int i = 0; i < len; i++) {
            ans += Math.max(left[i], right[i]);
        }
        return ans;
    }

    @Test
    public void test1() {
        int result = new Candy().candy(new int[]{1, 0, 2});
        Assert.assertEquals(result, 5);
    }

    @Test
    public void test2() {
        int result = new Candy().candy(new int[]{1, 2, 2});
        Assert.assertEquals(result, 4);
    }

    @Test
    public void test3() {
        int result = new Candy().candy(new int[]{1, 3, 2, 2, 1});
        Assert.assertEquals(result, 7);
    }
}
